﻿// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information.
// Website: https://admin.blazor.zone

using BlazorAdmin.Models;
using BlazorAdmin.Interface;
using BlazorAdmin.Extensions;
using System;
using System.Threading.Tasks;
using BlazorAdmin.Services.DbService;

namespace BlazorAdmin.Services
{
    public class LogService : BaMgmtDbService, ILog
    {
        public LogService(IPetaPoco database) : base(database) { }

        public void OperateLog(Log log)
        {
            log.Browser = Utils.GetClientBrowser(log.UserAgent);
            log.OS = Utils.GetClientOS(log.UserAgent);
            log.LogTime = DateTime.Now;

            Insert(log);
        }

        public async Task<bool> OperateLogAsync(Log log)
        {
            log.Browser = Utils.GetClientBrowser(log.UserAgent);
            log.OS = Utils.GetClientOS(log.UserAgent);
            log.LogTime = DateTime.Now;

            InsertAsync(log);

            return true;
        }

        public void LoginLog(LoginLog loginLog)
        {
            loginLog.Browser = Utils.GetClientBrowser(loginLog.UserAgent);
            loginLog.OS = Utils.GetClientOS(loginLog.UserAgent);
            loginLog.LogTime = DateTime.Now;
            loginLog.Result = loginLog.IsLoginIn ? "登录成功" : "登录失败";
                
            Insert(loginLog);
        }
    }
}